home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 687 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.3 KB

  1. From: James Kanze US/ESC 60/3/141 #40763 <kanze@lts.sel.alcatel.de>
  2. Message-ID: <9603120945.AA07641@lts.sel.alcatel.de>
  3. X-Original-Date: Tue, 12 Mar 96 10:45:48 +0100
  4. Path: in2.uu.net!bounce-back
  5. Date: 12 Mar 96 16:29:50 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Organization: -
  8. In-Reply-To: alice yang's message of 12 Mar 96 02:11:44 GMT
  9. Newsgroups: comp.std.c++
  10. Subject: Re: static_cast
  11. References: <30F54E6D.B4@worldnet.att.net>
  12. Followup-To: comp.lang.c++.moderated
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBFAgUBMUWmoOEDnX0m9pzZAQEy7QF/Za8cBp0qKl3OKjgSaIk+goeW8d+rS0P7
  15.     7sP4ADQtq5D55itAJ4DEvQaz0xyQcy4I
  16.     =Y+Rv
  17.  
  18. In article <30F54E6D.B4@worldnet.att.net> alice yang
  19. <aliceyang@worldnet.att.net> writes:
  20.  
  21. |>  Would you help me to understand the importance of using "static-cast"?
  22.  
  23. |> In the book, it states, "this cast construct is a better alternative than
  24. |> its c counterpart only becasue it is easier to locate a c++ cast construct
  25. |> in a source file"
  26.  
  27. |> So, does that mean the following is exactly the same?
  28.  
  29. |> float fnumber;
  30. |> int   inumber;
  31.  
  32. |> fnumber=(float)inumber;
  33. |> fnumber=<static_cast>inumber; /same as the first one??/
  34.  
  35. If you get the syntax right, yes:
  36.  
  37.     fnumber = static_cast< float >( inumber ) ;
  38.  
  39. Not all of the old style casts correspond to a static_cast, however.
  40. For instance:
  41.  
  42.     T*          p ;
  43.     (T1*)p              //  Use reinterpret_cast< T1* >( p )
  44.  
  45.     T const*    pc ;
  46.     (T*)pc              //  Use const_cast< T* >( p )
  47.     (T1*)pc           
  48.         //  Use reinterpret_cast< T1* >(const_cast< T* >( pc ) )
  49.  
  50. In practice, use static_cast for numeric conversions and to navigate a
  51. hierarchy, and const_cast to cast away const.  Don't use
  52. reinterpret_cast except in really special cases; it is almost always
  53. wrong in application software.  (It is present because certain types
  54. of system software, e.g. IO, cannot be written without it.)
  55.  
  56. Concerning the recommendation `in the book': I tend to use an explicit
  57. constructor call for value conversions, since in fact, the results are
  58. a new object:
  59.     
  60.     fnumber = (float)( inumber ) ;
  61.  
  62. (I systematically put the type in parentheses when there is only a
  63. single parameter, since this avoids the ambiguity that occasionally
  64. otherwise occurs between an explicit constructor and a declaration.
  65. Formally, the C++ grammar says that this is an old style cast, but in
  66. practice, it is exactly the same as an explicit constructor call.  And
  67. I *ONLY* do this where the results are a new object; I definitly avoid
  68. the old style casts in all othe contexts.  Come to think about it, I
  69. don't use the new style casts that often, either.)
  70.  
  71. Since this posting is mainly concerned with style, and not
  72. standardization issues, I've set the follow-ups to
  73. comp.lang.c++.moderated.
  74.  
  75. --
  76. James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
  77. GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
  78. Conseils, itudes et rialisations en logiciel orienti objet --
  79.                 -- A la recherche d'une activiti dans une region francophone
  80. ---
  81. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  82. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  83. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  84. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  85. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  86.